﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using System.Text;
using DotNet.Common;
using System.Collections.Generic;
public partial class Admin_EditProgram : basePage
{
    programClassBll pcBll = new programClassBll();
    ProgramBll programBll = new ProgramBll();
    productClassBll productCBll = new productClassBll();
    productBll pBll = new productBll();
    StringBuilder str = new StringBuilder();
    int selectIndex = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pcDt"] = pcBll.GetDataTable(0);
            if (Request.QueryString["action"] != null)
            {
                string action = Request.QueryString["action"];
                switch (action.Trim())
                {
                    case "edit":
                        Edit();
                        break;
                    case "appendProductId":
                        AppendProductIdDp();
                        break;
                    case "addProduct":
                        addProduct();
                        break;
                    case "save":
                        save();
                        break;
                    default:
                        break;
                }
            }
        }
    }

    #region 保存
    public void save() 
    {
        Program program = new Program();
        program.programId = Convert.ToInt32(Request.Form["programId"]);
        program.programName = Request.Form["programName"].ToString();
        if (program.programName == "")
        {
            Response.Write("项目名称不能为空！");
            Response.End();
        }
        program.programClassId = Convert.ToInt32(Request.Form["programClass"]);
        if (program.programClassId == 0)
        {
            Response.Write("请选择项目所属类别！");
            Response.End();
        }
        if (!CommUtil.IsNumStr(Request.Form["programSort"].ToString()) || Request.Form["programSort"].ToString() == "")
        {
            Response.Write("排序不能为空/必须为数字！");
            Response.End();
        }
        program.programSort = Convert.ToInt32(Request.Form["programSort"]);      
        program.productID = Request.Form["productId"].ToString();
        if (program.productID.Trim()=="")
        {
            Response.Write("请选择关联产品！");
            Response.End();
        }
        if (programBll.Update(program))
        {
            Response.Write("1");
            Response.End();
        }

    }
    #endregion

    #region 编辑
    public void Edit() 
    {
        int programid = Convert.ToInt32(Request.QueryString["pid"]);
        Program program = programBll.GetModel(programid);

        this.pid_hid.Value = program.productID.ToString();
        this.programId_hid.Value = programid.ToString();

        this.programName.Value = program.programName;
        selectIndex = program.programClassId;
        this.programSort.Value = program.programSort.ToString();       
        string product_str = "";
        if (program.productID.Contains("|"))
        {
            IList<product> proList = pBll.GetModel(program.productID.Split('|'));
            foreach (product pro in proList)
            {
                product_str += pro.productName + "◆";
            }
            product_str = product_str.Remove(product_str.LastIndexOf('◆'), 1);
        }
        else if(program.productID.Trim()!=""&&CommUtil.IsNumStr(program.productID))
        {
            int productid=Convert.ToInt32(program.productID);
            product productModel= pBll.GetModel(productid);
            product_str = productModel.productName;
        }
        this.content.Value = product_str;
    }
    #endregion

    #region 自定义下拉框
    public void programClassDp()
    {
        StringBuilder str = new StringBuilder();
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));
        Response.Write(str.ToString());

    }

    public string AddItems(int ParentID, int length, StringBuilder sb)
    {
        DataView dvTree = pcBll.GetDataView(ViewState["pcDt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            if (ParentID == 0)
            {
                sb.AppendFormat("<optgroup style=\"color:Red;\" label='" + row["programClassName"] + "'>{0}</optgroup>", str + "|--" + row["programClassName"]);
            }
            else
            {
                if (selectIndex == Convert.ToInt32(row["programClassId"]))
                {
                    sb.AppendFormat("<option selected='selected' value='" + row["programClassId"].ToString() + "'>{0}</option>", str + "|--" + row["programClassName"].ToString());
                }
                else
                {
                    sb.AppendFormat("<option value='" + row["programClassId"].ToString() + "'>{0}</option>", str + "|--" + row["programClassName"].ToString());
                }
            }
            AddItems(Convert.ToInt32(row["programClassId"]), length, sb);
        }
        return sb.ToString();

    }

    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp";
            }
        }
        return mark;
    }
    #endregion

    #region 产品类别选项
    public void ApendProductClassDp()
    {
        str.Remove(0, str.Length);
        IList<productClass> pcList = productCBll.GetModel();
        str.Append("<option value=\"0\" selected='selected'>--顶级类别--</option>");
        foreach (productClass pc in pcList)
        {
            str.Append("<option value=\"" + pc.pClassId + "\">&nbsp;&nbsp;&nbsp;" + pc.pClassName + "</option>");
        }
        Response.Write(str.ToString());
    }
    #endregion

    #region 追加产品选项
    public void AppendProductIdDp()
    {
        int pid = Convert.ToInt32(Request.QueryString["ProductID"]);
        if (pid > 0)
        {
            str.Remove(0, str.Length);
            IList<product> pList = pBll.pagingForProduct(1, 100, pid, 0, false);
            foreach (product p in pList)
            {
                str.Append("<option value=\"" + p.productId + "\">&nbsp;&nbsp;&nbsp;" + p.productName + "</option>");
            }
            if (pList.Count == 0)
            {
                str.Append("<option value=\"\">该类别下无产品</option>");
            }
            Response.Write(str.ToString());
            Response.End();
        }
        else
        {
            Response.Write("1");
            Response.End();
        }
    }
    #endregion

    #region 添加产品
    public void addProduct()
    {
        string productId_content = Request.Form["productId_content"].ToString();
        string productId_Id = Request.Form["productId_Id"].ToString();
        string addProductId = Request.Form["productId"].ToString();
        string addProductName = Request.Form["productName"].ToString();
        string newProductId_content = "";
        string newProductName_content = "";
        if (addProductId == "0")
        {
            Response.Write("1");
            Response.End();
        }
        if (productId_content.Contains("◆"))
        {
            bool bo = true;
            IList<string> productId_contentS = productId_content.Split('◆');
            foreach (string str in productId_contentS)
            {
                if (str == addProductName)
                {
                    bo = false;
                }
                else
                {
                    newProductName_content += str + "◆";
                }
            }
            if (bo)
            {
                newProductName_content += addProductName;
            }
            else
            {
                newProductName_content = newProductName_content.Remove(newProductName_content.LastIndexOf('◆'), 1);
            }
        }
        else
        {
            if (productId_content.Trim() != "")
            {
                if (productId_content != addProductName)
                {
                    newProductName_content += productId_content + "◆" + addProductName;
                }
            }
            else
            {
                newProductName_content = addProductName;
            }

        }
        if (productId_Id.Contains("|"))
        {
            bool bo = true;
            IList<string> productid_idL = productId_Id.Split('|');
            foreach (string str in productid_idL)
            {
                if (str == addProductId)
                {
                    bo = false;
                }
                else
                {
                    newProductId_content += str + "|";
                }
            }
            if (bo)
            {
                newProductId_content += addProductId;
            }
            else
            {
                newProductId_content = newProductId_content.Remove(newProductId_content.LastIndexOf('|'), 1);
            }
        }
        else
        {
            if (productId_Id.Trim() != "")
            {
                if (productId_Id.Trim() != addProductId)
                {
                    newProductId_content += productId_Id + "|" + addProductId;
                }
            }
            else
            {
                newProductId_content = addProductId;
            }
        }
        Response.Write(newProductName_content + "_" + newProductId_content);
        Response.End();
    }
    #endregion
}
